import axios from "axios";
import { Message } from "element-ui";
import router from "../../router";

axios.defaults.timeout = 5000 // 超时时间是5秒
axios.defaults.withCredentials = true // 允许跨域
// Context-Type 响应头
axios.defaults.headers.post['Context-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 基础URL
axios.defaults.baseURL = '/'

//请求拦截器
axios.interceptors.request.use(config=>{
  if(window.sessionStorage.getItem("tokenStr")){
    config.headers['Authorization'] = window.sessionStorage.getItem("tokenStr")
  }
  return config;
},error=>{
    Message.error({
        message: '请求超时!'
    })
    return Promise.reject(error)
})

//相应拦截器
axios.interceptors.response.use(success =>{
    if(success.status && success.status == 200){
        if(success.data.code == 500 || success.data.code == 401 || success.data.code == 403){
            Message.error({message:success.data.message});
            return;
        }
        if(success.data.message){
          
            Message.success({message:success.data.message});
        }
    }
    return success;  
}, error => {
    if(error.response.code == 504 || error.response.code == 404){
        Message.error({message:"服务器被吃了⊙﹏⊙∥"});
    }else if(error.response.code == 403){
        Message.error({message:"权限不足,请联系管理员!"});
    }else if(error.response.code == 401){
        Message.error({message:"登录已过期,请重新登录!"});
        router.replace("/");
    }else {
        if(error.response.data.message){
            Message.error({message:error.response.data.message});
        }else{
            Message.error({message:"未知错误!"});
        }
    }
    return;
});

let base = '';

/*
*封装get方法
*/
export function get (url, params) {
    return new Promise((resolve, reject) => {
      axios.get(base+url, {
        params: params}
      ).then(response => {
        resolve(response.data)
      }).catch(err => {
        reject(err)
      })
    })
  }
  
  /*
  *封装post方法
  */
  export function post (url, data) {
    return new Promise((resolve, reject) => {
      axios.post(base+url, data
      ).then(response => {
        resolve(response.data)
      }).catch(err => {
        reject(err)
      })
    })
  }
  
  
  /**
   * put方法，对应put请求
   * @param {String} url [请求的url地址]
   * @param {Object} params [请求时携带的参数]
   */
   export function put(url, params) {
    return new Promise((resolve, reject) => {
        axios.put(base+url, params)
            .then(response => {
                resolve(response.data);
                // Loading.service(true).close();
                //  Message({message: '请求成功', type: 'success'});
            })
            .catch(err => {
                reject(err)
            })
    });
  }
  
  
  /**
  * delete
  * @param {String} url [请求的url地址]
  * @param {Object} params [请求时携带的参数]
  */
  export function deletefn(url, params) {
    return new Promise((resolve, reject) => {
        axios.delete(base+url, params)
            .then(response => {
                resolve(response.data);
                // Loading.service(true).close();
                //  Message({message: '请求成功', type: 'success'});
            })
            .catch(err => {
                reject(err)
            })
    });
  }